import requests
from lxml import etree

# 制造请求头，将爬虫伪装成浏览器
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}

# 利用requests发出请求，并接收响应
response = requests.get('http://www.baidu.com',headers=headers)
'''
[200]
    -状态码 
    -200 访问成功
    -404，500，等待状态码都代表访问失败
'''
# 设定网页编码
response.encoding = 'utf-8'
# 获取网页的源代码
text = response.text

# 获取图片的地址
selector = etree.HTML(text)
src = selector.xpath('//*[@id="s_lg_img"]/@src')[0]
src = 'https:'+src
print(src)

# 下载百度的logo
response = requests.get(src,headers=headers)
# 获取图片
logo = response.content
# 保存
with open('python\day08\logo.gif','wb') as file:
    file.write(logo)
